﻿
#include "CreateVolumeDlg.h"
#include <vtkLineSource.h>
#include <vtkActor.h>    
#include <vtkPolyDataMapper.h>    



CreateVolumeDlg::CreateVolumeDlg(QWidget* parent):
    DlgBase(parent)
{
    initUI();
    manageConnections();
}

CreateVolumeDlg::~CreateVolumeDlg()
{
}

bool CreateVolumeDlg::create()
{
    auto pVtkViewWindow = MyVtkViewWindow::GetInstance();
    if(pVtkViewWindow == nullptr)
    {
        return false;
    }

    {
        m_startPointCoord->getValues(m_startPoint[0], m_startPoint[1], m_startPoint[2]);
        m_endPointCoord->getValues(m_endPoint[0], m_endPoint[1], m_endPoint[2]);

        //create vtk vtkLineSource
        vtkSmartPointer<vtkLineSource> lineSource = vtkSmartPointer<vtkLineSource>::New();
        lineSource->SetPoint1(m_startPoint);
        lineSource->SetPoint2(m_endPoint);
        lineSource->Update();

        vtkSmartPointer<vtkPolyDataMapper> lineMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
        lineMapper->SetInputConnection(lineSource->GetOutputPort());
        lineMapper->StaticOn();

        vtkSmartPointer<vtkActor> lineActor = vtkSmartPointer<vtkActor>::New();
        lineActor->SetMapper(lineMapper);
        lineActor->GetProperty()->SetColor(QtVtkModle::Global::VolumeColor[0], QtVtkModle::Global::VolumeColor[1], QtVtkModle::Global::VolumeColor[2]);  //set line color
        lineActor->GetProperty()->SetLineWidth(5);

        //add line to vtk view
        pVtkViewWindow->GetRenderer()->AddActor(lineActor);
        GetActorManagerInstance()->AddActor(lineActor, PickMode::EDGE);
    }

    pVtkViewWindow->GetRenderer()->ResetCamera();
    pVtkViewWindow->GetRenderWindow()->Render();
    return true;
}

void CreateVolumeDlg::initUI()
{
    setWindowTitle("Create Volume");
    QVBoxLayout* pDlgLayout = new QVBoxLayout(this);  //create layout

    {
        m_startPointCoord = new MyCoordinateEdit(this, "Start Point");
        pDlgLayout->addWidget(m_startPointCoord);

        m_endPointCoord = new MyCoordinateEdit(this, "End Point");
        m_endPointCoord->setValues(1, 1, 1);
        pDlgLayout->addWidget(m_endPointCoord);
    }

    {
        m_okBtn = new QPushButton("OK", this);
        m_cancelBtn = new QPushButton("Cancel", this);

        QHBoxLayout* okCancelLayout = new QHBoxLayout(this);
        okCancelLayout->addWidget(m_okBtn);
        okCancelLayout->addWidget(m_cancelBtn);
        pDlgLayout->addLayout(okCancelLayout);
    }

    this->setLayout(pDlgLayout);
}

void CreateVolumeDlg::manageConnections()
{
    connect(m_okBtn, &QPushButton::clicked, this, [=]()
            {
                if(create())
                {
                    close();
                }
                else
                {
                    QMessageBox::warning(this, "Error", "Failed to create volume!");
                }

            });

    connect(m_cancelBtn, &QPushButton::clicked, this, &CreateVolumeDlg::close);
}



